﻿using System.Text;
using System.Data;
using DataLib.Model;
using DataLib.DBUtility;

namespace DataLib.DAL
{
    /// <summary>
    /// 系统菜单管理类。(普通SQL实现方式)
    /// </summary>
    public class SysManage
    {
        //在这里可以更换数据库,支持多数据库，支持采用加密方式实现
        //DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.GetConnectionString("ConnectionString2"));
        public SysManage()
        {
        }

        public int GetMaxId()
        {
            string strsql = "select max(NodeID)+1 from S_Tree";
            object obj = DbHelperSQL.GetSingle(strsql);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }

        public int AddTreeNode(SysNode node)
        {
            node.NodeID = GetMaxId();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into S_Tree(");
            strSql.Append("NodeID,Text,ParentID,Location,OrderID,comment,Url,PermissionID,ImageUrl)");
            strSql.Append(" values (");
            strSql.Append("'" + node.NodeID + "',");
            strSql.Append("'" + node.TreeText + "',");
            strSql.Append("" + node.ParentID + ",");
            strSql.Append("'" + node.Location + "',");
            strSql.Append("" + node.OrderID + ",");
            strSql.Append("'" + node.Comment + "',");
            strSql.Append("'" + node.Url + "',");
            strSql.Append("" + node.PermissionID + ",");
            strSql.Append("'" + node.ImageUrl + "'");
            //			strSql.Append(""+node.ModuleID+",");
            //			strSql.Append(""+node.KeShiDM+",");
            //			strSql.Append("'"+node.KeshiPublic+"'");
            strSql.Append(")");
            DbHelperSQL.ExecuteSql(strSql.ToString());
            return node.NodeID;

        }

        public void UpdateNode(SysNode node)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update S_Tree set ");
            strSql.Append("Text='" + node.TreeText + "',");
            strSql.Append("ParentID=" + node.ParentID.ToString() + ",");
            strSql.Append("Location='" + node.Location + "',");
            strSql.Append("OrderID=" + node.OrderID + ",");
            strSql.Append("comment='" + node.Comment + "',");
            strSql.Append("Url='" + node.Url + "',");
            strSql.Append("PermissionID=" + node.PermissionID + ",");
            strSql.Append("ImageUrl='" + node.ImageUrl + "'");
            //			strSql.Append("ModuleID="+node.ModuleID+",");
            //			strSql.Append("KeShiDM="+node.KeShiDM+",");
            //			strSql.Append("KeshiPublic='"+node.KeshiPublic+"'");
            strSql.Append(" where NodeID=" + node.NodeID);
            DbHelperSQL.ExecuteSql(strSql.ToString());

        }

        public void DelTreeNode(int nodeid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete S_Tree ");
            strSql.Append(" where NodeID=" + nodeid);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }


        public DataSet GetTreeList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from S_Tree ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by parentid,orderid ");

            return DbHelperSQL.Query(strSql.ToString());
        }

        public int GetPermissionCatalogID(int permissionID)
        {
            string sql = "select CategoryID from Accounts_Permissions where PermissionID=" + permissionID;
            object res = DbHelperSQL.GetSingle(sql);
            if (res == null)
            {
                return 0;
            }
            return (int)res;
        }
        /// <summary>
        /// 得到菜单节点
        /// </summary>
        /// <param name="NodeID"></param>
        /// <returns></returns>
        public SysNode GetNode(int NodeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from S_Tree ");
            strSql.Append(" where NodeID=" + NodeID);
            SysNode node = new SysNode();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                node.NodeID = int.Parse(ds.Tables[0].Rows[0]["NodeID"].ToString());
                node.TreeText = ds.Tables[0].Rows[0]["text"].ToString();
                if (ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    node.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                node.Location = ds.Tables[0].Rows[0]["Location"].ToString();
                if (ds.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    node.OrderID = int.Parse(ds.Tables[0].Rows[0]["OrderID"].ToString());
                }
                node.Comment = ds.Tables[0].Rows[0]["comment"].ToString();
                node.Url = ds.Tables[0].Rows[0]["url"].ToString();
                if (ds.Tables[0].Rows[0]["PermissionID"].ToString() != "")
                {
                    node.PermissionID = int.Parse(ds.Tables[0].Rows[0]["PermissionID"].ToString());
                }
                node.ImageUrl = ds.Tables[0].Rows[0]["ImageUrl"].ToString();

                return node;
            }
            else
            {
                return null;
            }
        }

        #region 日志
        /// <summary>
        /// 增加日志
        /// </summary>
        /// <param name="time"></param>
        /// <param name="loginfo"></param>
        public void AddLog(string time, string loginfo, string Particular)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into S_Log(");
            strSql.Append("datetime,loginfo,Particular)");
            strSql.Append(" values (");
            strSql.Append("'" + time + "',");
            strSql.Append("'" + loginfo + "',");
            strSql.Append("'" + Particular + "'");
            strSql.Append(")");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        public void DeleteLog(int? id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete S_Log ");
            strSql.Append(" where ID= " + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        public void DelOverdueLog(int days)
        {
            string str = " DATEDIFF(day,[datetime],getdate())>" + days;
            DeleteLog(str);
        }
        public void DeleteLog(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete S_Log ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        public DataSet GetLogs(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from S_Log ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by ID DESC");
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataRow GetLog(string ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from S_Log ");
            strSql.Append(" where ID= " + ID);
            return DbHelperSQL.Query(strSql.ToString()).Tables[0].Rows[0];
        }
        #endregion

    }
}
